import { Body, Controller, ParseIntPipe, Post, Req } from '@nestjs/common';
import { UserService } from './user.service';
import { RefuseDto } from './dto/refuse.dto';
import { ResponseUtilService } from '@app/response-util';
import { DtoPipe } from '../../pipe/dto/dto.pipe';
import PageDto from './dto/page.dto';
import { User } from '@app/db/entity/User';

@Controller('user')
export class UserController {
    constructor(
        private readonly service:UserService
    ){}

    @Post('page')
    async superUserPage(@Body(new DtoPipe({skipMissingProperties : true})) dto:PageDto, @Req() req) {
        const r = await this.service.page(dto)
        return ResponseUtilService.successRes(r)
    }

    @Post('add')
        async add(@Body() dto:User, @Req() req) {
        try {
            const r = await this.service.add(dto)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            console.log(error)
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('update')
    async update(@Body() dto:User, @Req() req) {
        try {
            const r = await this.service.update(dto)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('delete')
    async del(@Body('id', ParseIntPipe) id:number, @Req() req) {
        if(id < 1) return null
        try {
            const r = await this.service.del(id)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        } 
    }
    
}
